//
//  DKHomeLoanScoreAlertView.m
//  DanKU
//
//  Created by 吴斌 on 2023/12/8.
//

#import "DKHomeLoanScoreAlertView.h"
#import <StoreKit/StoreKit.h>

@interface DKHomeLoanScoreAlertView()
@property (nonatomic, weak)zhPopupController * dk_HomeLoanScoreAlertPop;
@property (copy, nonatomic) void (^finishCompletion)(void);

@property (nonatomic, strong)UITextView * dk_loanScoreTextView;
@property (nonatomic, strong)UIButton * dk_LoanScoreBtn;

@property (nonatomic, copy)NSString * cureenRate;
@end
@implementation DKHomeLoanScoreAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 50;
        self.clipsToBounds = YES;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel *dk_LoanScoreHeadLab = [[UILabel alloc] init];
    dk_LoanScoreHeadLab.textColor = DK_Hex(@"#333333");
    dk_LoanScoreHeadLab.font = DK_FONT_Medium(17);
    dk_LoanScoreHeadLab.text = @"Please rate this app";
    [self addSubview:dk_LoanScoreHeadLab];
    [dk_LoanScoreHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    UIButton * dk_LoanScoreCloseBtn = [[UIButton alloc] init];
    [dk_LoanScoreCloseBtn setBackgroundImage:DK_GetImage(@"dk_LoginAgreementClose") forState:UIControlStateNormal];
    [dk_LoanScoreCloseBtn addTarget:self action:@selector(goCancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_LoanScoreCloseBtn];
    [dk_LoanScoreCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.width.equalTo(@(DK_IsWidthScale(17)));
    }];
    for (int i = 0; i < 5; i++) {
        UIButton * dk_LoanScoreStartBtn = [[UIButton alloc] init];
        [dk_LoanScoreStartBtn setBackgroundImage:DK_GetImage(@"dk_LoanDetailStartNormal") forState:UIControlStateNormal];
        [dk_LoanScoreStartBtn setBackgroundImage:DK_GetImage(@"dk_LoanDetailStartSelect") forState:UIControlStateSelected];
        dk_LoanScoreStartBtn.tag = 100+i;
        [dk_LoanScoreStartBtn addTarget:self action:@selector(tapLoanScore:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:dk_LoanScoreStartBtn];
        CGFloat startBtnX = DK_IsWidthScale(38)+DK_IsWidthScale(51)*i;
        [dk_LoanScoreStartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(startBtnX));
            make.top.equalTo(dk_LoanScoreHeadLab.mas_bottom).offset(DK_IsWidthScale(30));
            make.width.equalTo(@(DK_IsWidthScale(36)));
            make.height.equalTo(@(DK_IsWidthScale(36)));
        }];
    }
    UIView *dk_loanScoreDescribeView = [[UIView alloc] init];
    dk_loanScoreDescribeView.backgroundColor = DK_Hex(@"#FAFAFA");
    [self addSubview:dk_loanScoreDescribeView];
    [dk_loanScoreDescribeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(22)));
        make.right.equalTo(@(-DK_IsWidthScale(22)));
        make.top.equalTo(dk_LoanScoreHeadLab.mas_bottom).offset(DK_IsWidthScale(96));
        make.height.equalTo(@(DK_IsWidthScale(110)));
    }];
    //占位符
    self.dk_loanScoreTextView = [[UITextView alloc] init];
    self.dk_loanScoreTextView.textColor = DK_Hex(@"#333333");
    self.dk_loanScoreTextView.font = DK_FONT_Regular(14);
    self.dk_loanScoreTextView.backgroundColor = UIColor.clearColor;
    [dk_loanScoreDescribeView addSubview:self.dk_loanScoreTextView];
    [self.dk_loanScoreTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(10)));
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.top.equalTo(@(DK_IsWidthScale(10)));
        make.bottom.equalTo(@(-DK_IsWidthScale(30)));
    }];
    UILabel* loanScorePlaceHolderLab = [[UILabel alloc] init];
    loanScorePlaceHolderLab.textColor = DK_Hex(@"#CCCCCC");
    loanScorePlaceHolderLab.font = DK_FONT_Regular(14);
    loanScorePlaceHolderLab.text = @"Please write your comments\n(opcional)";
    loanScorePlaceHolderLab.numberOfLines = 0;
    [loanScorePlaceHolderLab sizeToFit];
    [self.dk_loanScoreTextView addSubview:loanScorePlaceHolderLab];
    [self.dk_loanScoreTextView setValue:loanScorePlaceHolderLab forKey:@"_placeholderLabel"];
    
    self.dk_LoanScoreBtn = [[UIButton alloc] init];
    [self.dk_LoanScoreBtn setTitle:@"OK" forState:UIControlStateNormal];
    [self.dk_LoanScoreBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    [self.dk_LoanScoreBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    self.dk_LoanScoreBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_LoanScoreBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_LoanScoreBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    [self.dk_LoanScoreBtn addTarget:self action:@selector(goLoanScore) forControlEvents:UIControlEventTouchUpInside];
    self.dk_LoanScoreBtn.enabled = NO;
    self.dk_LoanScoreBtn.layer.cornerRadius = DK_IsWidthScale(25);
    self.dk_LoanScoreBtn.clipsToBounds = YES;
    [self addSubview:self.dk_LoanScoreBtn];
    [self.dk_LoanScoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(dk_loanScoreDescribeView.mas_bottom).offset(DK_IsWidthScale(40));
    }];
}
- (void)tapLoanScore:(UIButton*)sender{
    UIButton * dk_LoanScoreStartFristBtn = [self viewWithTag:100];
    UIButton * dk_LoanScoreStartSecondBtn = [self viewWithTag:101];
    UIButton * dk_LoanScoreStartThreeBtn = [self viewWithTag:102];
    UIButton * dk_LoanScoreStartFourBtn = [self viewWithTag:103];
    UIButton * dk_LoanScoreStartFiveBtn = [self viewWithTag:104];
    if(sender.tag == 104){
        dk_LoanScoreStartFristBtn.selected = YES;
        dk_LoanScoreStartSecondBtn.selected = YES;
        dk_LoanScoreStartThreeBtn.selected = YES;
        dk_LoanScoreStartFourBtn.selected = YES;
        dk_LoanScoreStartFiveBtn.selected = YES;
    }else if(sender.tag == 103){
        dk_LoanScoreStartFristBtn.selected = YES;
        dk_LoanScoreStartSecondBtn.selected = YES;
        dk_LoanScoreStartThreeBtn.selected = YES;
        dk_LoanScoreStartFourBtn.selected = YES;
        dk_LoanScoreStartFiveBtn.selected = NO;
    }else if(sender.tag == 102){
        dk_LoanScoreStartFristBtn.selected = YES;
        dk_LoanScoreStartSecondBtn.selected = YES;
        dk_LoanScoreStartThreeBtn.selected = YES;
        dk_LoanScoreStartFourBtn.selected = NO;
        dk_LoanScoreStartFiveBtn.selected = NO;
    }else if(sender.tag == 101){
        dk_LoanScoreStartFristBtn.selected = YES;
        dk_LoanScoreStartSecondBtn.selected = YES;
        dk_LoanScoreStartThreeBtn.selected = NO;
        dk_LoanScoreStartFourBtn.selected = NO;
        dk_LoanScoreStartFiveBtn.selected = NO;
    }else if(sender.tag == 100){
        dk_LoanScoreStartFristBtn.selected = YES;
        dk_LoanScoreStartSecondBtn.selected = NO;
        dk_LoanScoreStartThreeBtn.selected = NO;
        dk_LoanScoreStartFourBtn.selected = NO;
        dk_LoanScoreStartFiveBtn.selected = NO;
    }
    self.cureenRate = [NSString stringWithFormat:@"%ld",(long)(sender.tag-99)];
    self.dk_LoanScoreBtn.enabled = YES;
}
- (void)goLoanScore{
    WeakSelf(self)
    if([weakSelf.cureenRate isEqualToString:@"5"]){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            [SKStoreReviewController requestReview];
        });
    }
    NSMutableDictionary *loanScoreDic = [[NSMutableDictionary alloc] init];
    [loanScoreDic setValue:self.cureenRate forKey:@"score"];
    [loanScoreDic setValue:self.dk_loanScoreTextView.text forKey:@"suggest"];
    [DKPublicHttp apiUserSubmitRateForAPI:loanScoreDic complete:^{
        if(![weakSelf.cureenRate isEqualToString:@"5"]){
            [DKAlertHelper showMessage:@"Thank you for your advice, we will continue to improve our service"];
        }else{
            if(weakSelf.finishCompletion){
                weakSelf.finishCompletion();
            }
        }
        [weakSelf.dk_HomeLoanScoreAlertPop dismiss];
    }];
}
- (void)goCancle{
    [self.dk_HomeLoanScoreAlertPop dismiss];
}
+ (void)dk_ShowLoanScoreAlertView:(UIView *)keyView complete:(void(^)(void))complete{
    DKHomeLoanScoreAlertView * dk_HomeLoanScoreAlertView = [[DKHomeLoanScoreAlertView alloc] init];
    zhPopupController * homeLoanScoreAlertCtrl = [[zhPopupController alloc] initWithView:dk_HomeLoanScoreAlertView size:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), DK_IsWidthScale(390))];
    homeLoanScoreAlertCtrl.presentationStyle = zhPopupSlideStyleTransform;
    homeLoanScoreAlertCtrl.layoutType = zhPopupLayoutTypeCenter;
    homeLoanScoreAlertCtrl.presentationTransformScale = 0.56;
    homeLoanScoreAlertCtrl.dismissonTransformScale = 0.56;
    homeLoanScoreAlertCtrl.dismissOnMaskTouched = NO;
    dk_HomeLoanScoreAlertView.dk_HomeLoanScoreAlertPop = homeLoanScoreAlertCtrl;
    dk_HomeLoanScoreAlertView.finishCompletion = complete;
    [homeLoanScoreAlertCtrl showInView:keyView completion:^{
    }];
}

@end
